define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    require('../su/accordion/oaSearchCurtainCtrl');

    $.widget('rk.approvalCurtainCtrl', $.rk.oaSearchCurtainCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var html = rk.templateText(require('oa/tmpl/approval/tpl_approval_curtain_search_approvals.tpl'), {viewType: opt.viewType});
            elem.html(html);
            me._initSearchForm();
            elem.find('a.pg-btn-blue').click(function () {
                me._handleAdvSearchClick($(this).attr('viewType'), me);
            });
            elem.on('click', 'a[act="closeApprovalSearchCurtain"]', function () {
                me.closeMe();
            });
            elem.find('.oa-curtain-body:first').beScroll();
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },
        _initSearchForm: function () {
            var me = this;
            var elem = me.element;
            rk.post('/json/oa_approval/init-search-form.action', {}).done(function (json) {
                if (json.status == 0) {
                    $('div[_id="approvalStatusCheck"]').buttonset();
                    $('div[_id="approvalDateRadio"]').buttonset();
                    $('div[_id="approvalDateRadio"]').find('input[name="applyDateType"]').click(function (event) {
                        var dateType = $(this).attr("dateType");
                        $('input[_id="applyDateTypeH"]').val(dateType);
                        if('other' == dateType){
                            $('tr[_id="date_defined"]').show();
                            me._repositionList();
                        }else{
                            $('tr[_id="date_defined"]').hide();
                            me._repositionList();
                        }
                    });
                    var flowCategoryDom = $('select[_id="flowCategoryId"]');
                    $.each(json.data.flowCategories, function (i, o) {
                        flowCategoryDom.append('<option value="' + o.id + '">' + rk.htmlEscape(o.name) + '</option>');
                    });
                    flowCategoryDom.selectmenu();
                    $('#departSelect').multitreeselector({source: '/json/crm_dimension/dim-oa-departs.action'});
                    $('input[_id="approvalUser"]').multipeopleselector({
                        position: {
                            my: 'left top+2'
                        }
                    });
                    $('input[_id="searchStartDate"]').val(json.data.startDate);
                    $('input[_id="searchEndDate"]').val(json.data.endDate);
                    $('tr[_id="date_defined"]').find('td input').datepicker({changeMonth: true, changeYear: true});
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _handleAdvSearchClick: function (viewType, me) {
            var applyDateType = $('input[_id="applyDateTypeH"]').val();
            var searchStartDate = $('input[_id="searchStartDate"]').val();
            var searchEndDate = $('input[_id="searchEndDate"]').val();
            if (applyDateType == 'other' && searchStartDate > searchEndDate) {
                $.msg(rk.i18n('VALIDATE_DATE_COMPARE_ERROR'));
                return false;
            }
            var statusArray = [];
            $('input[name="status"]:checked').each(function () {
                statusArray.push($(this).val());
            });
            var url;
            var param;
            if (viewType == 1) {
                url = '/json/oa_approval/search-my-submit-list.action';
                param = {
                    'approvalStatus': statusArray.join(","),
                    'flowCategoryId': $('select[_id="flowCategoryId"]').val(),
                    'oddNumber': $('input[_id="oddNumber"]').val(),
                    'applyDateType': applyDateType,
                    'startDate': searchStartDate,
                    'endDate': searchEndDate,
                    'title': $('input[_id="title"]').val()
                }
            } else if (viewType == 2) {
                url = '/json/oa_approval/search-submit-me-list.action';
                var userArray = [];
                $.each($('input[_id="approvalUser"]').multipeopleselector('val'), function (i, o) {
                    userArray.push(o.value);
                });
                param = {
                    'approvalStatus': statusArray.join(","),
                    'flowCategoryId': $('select[_id="flowCategoryId"]').val(),
                    'oddNumber': $('input[_id="oddNumber"]').val(),
                    'applyDateType': applyDateType,
                    'startDate': searchStartDate,
                    'endDate': searchEndDate,
                    'userIds': (userArray.length < 1 ? '' : userArray.join(",")),
                    'title': $('input[_id="title"]').val()
                }
            } else if (viewType == 3) {
                url = '/json/oa_approval/search-all-approval-list.action';
                var departArray = [];
                $.each($('#departSelect').multitreeselector('val'), function (i, o) {
                    departArray.push(o.value);
                });
                var userArray = [];
                $.each($('input[_id="approvalUser"]').multipeopleselector('val'), function (i, o) {
                    userArray.push(o.value);
                });
                param = {
                    'approvalStatus': statusArray.join(","),
                    'flowCategoryId': $('select[_id="flowCategoryId"]').val(),
                    'oddNumber': $('input[_id="oddNumber"]').val(),
                    'applyDateType': applyDateType,
                    'startDate': searchStartDate,
                    'endDate': searchEndDate,
                    'departIds': (departArray.length < 1 ? '' : departArray.join(",")),
                    'userIds': (userArray.length < 1 ? '' : userArray.join(",")),
                    'title': $('input[_id="title"]').val()
                }
            }
            //加载中
            $('div[_id="search-approval-list"]').beLoading();
            me._loadSearchData(viewType, url, param, 1);
        },
        _loadSearchData: function (viewType, url, param, pageNo) {
            var me = this;
            var elem = me.element;
            param.pageNo = pageNo;
            rk.post(url, param).done(function (json) {
                if (json.status == 0) {
                    $.each(json.data.users, function (i, o) {
                        if (o.icon == '') {
                            o.icon = DEFAULTS.USER_DEFAULT_ICON;
                        }
                    });
                    me._renderCurtain(viewType, json.data, url, param);
                    //$("#applyDetailDiv").html('<div class="approval_detail_none"></div>');
                    //$("#resultCountSpan").text($("#resultCount").val());
                    //$('#adv_search_result').show();
                    //$('#advConcelBtn').unbind("click").click(function () {
                    //    $("#cancleSearchBtn").click();
                    //});

                    me._repositionList();
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _renderCurtain: function (viewType, data, url, param) {
            var me = this;
            var elem = me.element;
            var listHtml;
            $('div.oa-tab-header').show();
            $('span[_id="searchCountSpan"]').html(data.totalCount);
            if (data.totalCount > 0) {
                if (viewType == 1) {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_my_submit.tpl'), data);
                } else if (viewType == 2) {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_submit_me.tpl'), data);
                } else if (viewType == 3) {
                    listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_all_approval.tpl'), data);
                }
            } else {
                listHtml = rk.templateText(require('oa/tmpl/approval/tpl_approval_panel_default_list.tpl'), {searchResult: 1});
            }
            $('div[_id="search-approval-list"]').html(listHtml);
            $('div.approval-list-pagination').rkPaging({
                hideForward: 1,
                pagenum: data.pageNo - 0,
                pagesize: data.pageSize - 0,
                rowscount: data.totalCount - 0,
                onChange: function(pageNo) {
                    me._loadSearchData(viewType, url, param, pageNo);
                }
            });
        }
    });

    return $;
});